package com.abe.libcore.base.screen.j;

import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;

import com.abe.libcore.base.screen.RelayoutTool;
import com.abe.libcore.utils.CommonUtils;

public abstract class BJScreenActivity extends AppCompatActivity {
    //是否需要自适应布局
    public abstract Boolean isAutoZoom();

    //是否正常设置标准宽度
    private Boolean isScreenSetNormal = false;

    //获取标准宽度
    int getStandardW() {
        Integer designWidth = (Integer) CommonUtils.INSTANCE.getMetaData(BJScreenActivity.this, "designWidth", 0);
        isScreenSetNormal = designWidth != null;
        return isScreenSetNormal ? designWidth : 0;
    }

    //获取缩放比例
    Float getScale() {
        DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
        int width = displayMetrics.widthPixels;
        int designWidth = getStandardW();
        return (isAutoZoom() && isScreenSetNormal) ? ((float) width / (float) designWidth) : 1.0F;
    }

    @Override
    public void setContentView(int layoutResID) {
        View view = View.inflate(this, layoutResID, null);
        this.setContentView(view);
    }

    @Override
    public void setContentView(View view) {
        CommonUtils.INSTANCE.log("scale:" + getScale());
        if (isAutoZoom() && getScale() != 1.0F) {
            RelayoutTool.INSTANCE.relayoutViewHierarchy(view, getScale());
        }
        super.setContentView(view);
    }

    @Override
    public void setContentView(View view, ViewGroup.LayoutParams params) {
        if (isAutoZoom()) {
            RelayoutTool.INSTANCE.relayoutViewHierarchy(view, getScale());
            RelayoutTool.INSTANCE.scaleLayoutParams(params, getScale());
        }
        super.setContentView(view, params);
    }
}